let db = require("../db/index");
// 车辆管理
exports.allCar = (req, res) => {
    var sql =
        'select carManagement.*,Owner,Phone from carManagement,room where room.RoomID=carManagement.RoomID ';
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
//修改车辆数据
exports.updateCar = (req, res) => {
    console.log(req.body);
    const { carNum, owner, carBrand, carColor, carNumType, imageUrl, id, flag } = req.body
    var sql =
        `select RoomID from room where Owner='${owner}'`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        if (!data.length) {
            res.send({
                message: '该业主不存在'
            })
            return
        }
        if (flag) {
            var sql =
                `update carmanagement set CarNum='${carNum}',RoomID=${data[0].RoomID},CarBrand='${carBrand}',CarColor='${carColor}',CarImg='${imageUrl}',CarNumType='${carNumType}' where ID=${id}`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send({
                    status: 200
                });
            });
            return
        }
        var sql =
            `insert into carmanagement(CarNum,RoomID,CarBrand,CarColor,CarImg,CarNumType)values('${carNum}',${data[0].RoomID},'${carBrand}','${carColor}','${imageUrl}','${carNumType}');`;
        db.query(sql, [], (err, data) => {
            if (err) {
                console.log("出错了", err);
                return res.send("错误" + err.message);
            }
            res.send({
                status: 200
            });
        });
    });
};